package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.model.validation.UserValidator;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class UserRegistrationPanel extends JPanel implements Loggable {

    public UserRegistrationPanel() {
        initComponents();
        UIUtils.correctTitleBorderFont(jPanel1);
        UIUtils.correctTitleBorderFont(jPanel2);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtFirstName = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtLastName = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtMobile = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtEmail = new ge.telasi.tasks.ui.comp.SuggestionText();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        pnStructure = new ge.telasi.tasks.ui.structure.StructureSelector();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtPosition = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtLocation = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtWorkPhone = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtUsername = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtPassword = new ge.telasi.tasks.ui.comp.SuggestionPassword();
        txtPassword2 = new ge.telasi.tasks.ui.comp.SuggestionPassword();

        jLabel1.setText("მომხმარებლის სახელი:");

        jLabel2.setText("პაროლი:");

        jLabel3.setText("დაადასტურება:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("პირადი ინფორმაცია"));

        jLabel4.setText("სახელი:");

        jLabel5.setText("გვარი:");

        jLabel6.setText("მობილური:");

        jLabel7.setText("ელ. ფოსტა:");

        txtFirstName.setName("User.firstName"); // NOI18N
        txtFirstName.setSuggestion("თქვენი სახელი");

        txtLastName.setName("User.lastName"); // NOI18N
        txtLastName.setSuggestion("თქვენი გვარი");

        txtMobile.setName("User.mobile"); // NOI18N
        txtMobile.setSuggestion("თქვენი მობილურის ნომერი");

        txtEmail.setName("User.email"); // NOI18N
        txtEmail.setSuggestion("თქვენი ელ. ფოსტის მისამართი");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMobile, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                    .addComponent(txtLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("კორპორატიული ინფორმაცია"));

        jLabel8.setText("განსაზღვრეთ სტრუქტურული ერთეული, რემელსაც მიეკუთვნებით:");

        pnStructure.setName("User.structure"); // NOI18N

        jLabel9.setText("თანამდებობა:");

        jLabel10.setText("ადგილმდებარეობა:");

        jLabel11.setText("შიდა ტელეფონი:");

        txtPosition.setName("User.position"); // NOI18N
        txtPosition.setSuggestion("თქვენი თანამდებობა კომპანიაში");

        txtLocation.setName("User.location"); // NOI18N
        txtLocation.setSuggestion("თქვენი ოთახის, ოფისის მდებარეობა");

        txtWorkPhone.setName("User.workPhone"); // NOI18N
        txtWorkPhone.setSuggestion("შიდა ტელეფონი");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnStructure, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(txtLocation, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtWorkPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtPosition, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnStructure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(txtLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtWorkPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        txtUsername.setName("User.username"); // NOI18N
        txtUsername.setSuggestion("აარჩიეთ მომხმარებლის სახელი");

        txtPassword.setName("User.password"); // NOI18N
        txtPassword.setSuggestion("ჩაწერეთ პაროლი");

        txtPassword2.setName("User.password2"); // NOI18N
        txtPassword2.setSuggestion("დაადასტურეთ პაროლი");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtPassword, txtPassword2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private ge.telasi.tasks.ui.structure.StructureSelector pnStructure;
    private ge.telasi.tasks.ui.comp.SuggestionText txtEmail;
    private ge.telasi.tasks.ui.comp.SuggestionText txtFirstName;
    private ge.telasi.tasks.ui.comp.SuggestionText txtLastName;
    private ge.telasi.tasks.ui.comp.SuggestionText txtLocation;
    private ge.telasi.tasks.ui.comp.SuggestionText txtMobile;
    private ge.telasi.tasks.ui.comp.SuggestionPassword txtPassword;
    private ge.telasi.tasks.ui.comp.SuggestionPassword txtPassword2;
    private ge.telasi.tasks.ui.comp.SuggestionText txtPosition;
    private ge.telasi.tasks.ui.comp.SuggestionText txtUsername;
    private ge.telasi.tasks.ui.comp.SuggestionText txtWorkPhone;
    // End of variables declaration//GEN-END:variables

    private Logger logger;

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] {
            txtUsername, txtPassword, txtPassword2,
            txtFirstName, txtLastName, txtMobile, txtEmail,
            pnStructure, txtPosition, txtLocation,
            txtWorkPhone
        };
    }

    public User getUser() {
        User user = new User();
        user.setUsername(txtUsername.getText());
        user.setPassword(new String(txtPassword.getPassword()));
        user.setFirstName(txtFirstName.getText());
        user.setLastName(txtLastName.getText());
        user.setMobile(txtMobile.getText());
        user.setEmail(txtEmail.getText());
        user.setStructure(pnStructure.getStructure());
        user.setPosition(txtPosition.getText());
        user.setLocation(txtLocation.getText());
        user.setWorkPhone(txtWorkPhone.getText());
        return user;
    }

    public String getPassword() {
        return new String(txtPassword.getPassword());
    }

    public boolean validateForm() {
        LoggerUtils.clearLogger(getLogger(), allFields());
        try {
            User usr = getUser();
            String password = new String(txtPassword.getPassword());
            String password2 = new String(txtPassword2.getPassword());
            new UserValidator().validateFull_client_create(usr, password, password2);
            return true;
        } catch (TaskException te) {
            LoggerUtils.manageException(this, getLogger(), te, allFields());
            return false;
        }
    }

}
